package top.lshaci.framework.common.exception;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import top.lshaci.framework.common.constants.ErrorInfo;

/**
 * <p>Framework common base exception</p> <br>
 *
 * <b>1.0.4: </b>Add field code <br>
 *
 * @author lshaci
 * @version 1.0.9
 * @since 0.0.1
 */
public class BaseException extends RuntimeException {

    private static final long serialVersionUID = -8170023045088441807L;

    /**
     * 异常码
     */
    @Getter
    private int code;

    /**
     * 根据 异常码, 异常信息 创建一个基础异常
     *
     * @param code 异常码
     * @param msg  异常信息
     * @since 1.0.9
     */
    public BaseException(int code, String msg) {
        super(msg);
        this.code = code;
    }

    /**
     * 根据 异常信息 创建一个基础异常
     *
     * @param errorInfo 异常信息
     * @since 1.0.4
     */
    public BaseException(ErrorInfo errorInfo) {
        this(errorInfo.getCode(), errorInfo.getMsg());
    }

    /**
     * 根据 异常码, 异常信息, 异常原因 创建一个基础异常
     *
     * @param errorInfo 异常信息
     * @param cause     异常原因
     * @since 1.0.9
     */
    public BaseException(ErrorInfo errorInfo, Throwable cause) {
        this(errorInfo);
        super.initCause(cause);
    }


    @Override
    public String toString() {
        return StrUtil.format("{}(code={}, message={})", this.getClass().getSimpleName(), this.code, super.getMessage());
    }

}
